﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;

using Kwan.Data;
using BG.Data;

namespace BG.Service
{
    public class ConsumeQuery
	{

		public static Expression MakeSearchWhere(NameValueCollection ps)
		{
			Expression exp = new Expression();

            string propName = "EntityID";
            if (!string.IsNullOrEmpty(ps[propName]))
                exp &= TConsume.entityID == int.Parse(ps[propName]);

            propName = "createdDate";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TConsume.createdDate == DateTime.Parse(ps[propName]);
            }
            propName = "item";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TConsume.item == ps[propName];
            }
            propName = "nett";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TConsume.nett == double.Parse(ps[propName]);
            }
            propName = "orderID";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TConsume.orderID == int.Parse(ps[propName]);
            }
            propName = "price";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TConsume.price == double.Parse(ps[propName]);
            }
            propName = "productID";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TConsume.productID == int.Parse(ps[propName]);
            }
            propName = "roomID";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TConsume.roomID == int.Parse(ps[propName]);
            }
            propName = "status";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TConsume.status == int.Parse(ps[propName]);
            }
            propName = "total";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TConsume.total == double.Parse(ps[propName]);
            }
            propName = "type";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TConsume.type == int.Parse(ps[propName]);
            }
            propName = "userID";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TConsume.userID == ps[propName];
            }

            propName = "tag";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                string val = ps[propName];
                string[] vals = val.Split(',');
                if (!string.IsNullOrEmpty(vals[0]))
                {
                    exp &= VConsume.roomNO == vals[0];
                }
                bool startNull = string.IsNullOrEmpty(vals[1]);
                bool endNull = string.IsNullOrEmpty(vals[2]);
                if (!startNull && !endNull)
                {
                    DateTime dt1 = DateTime.Parse(vals[1] + " 6:00:00");
                    DateTime dt2 = DateTime.Parse(vals[2] + " 6:00:00").AddDays(1);
                    exp &= VConsume.createdDate.Between(dt1, dt2);
                }
                else if (startNull && !endNull)
                {
                    exp &= VConsume.createdDate <= DateTime.Parse(vals[2] + " 6:00:00").AddDays(1);
                }
                else if (!startNull && endNull)
                {
                    exp &= VConsume.createdDate >= DateTime.Parse(vals[1] + " 6:00:00");
                }
            }

			return exp;
		}
	}
}